<?php
/**
 * [1,2,2,4,5]  [1,2,1,1,-1]
 * 找数组中距离下一个较大值的距离，没有返回-1
 * Created by PhpStorm.
 * User: Bruce
 * Date: 2020/5/19
 * Time: 12:31
 */
 function zhan($array)
{
    $stack = [];//栈
    $result = [];//结果
    //初始化结果
    for($j=0; $j<count($array); $j++){
        $result[$array[$j]] = -1;
    }

    for ($i=0;$i<count($array);$i++) {
        $k=0;
        while(!empty($stack) && end($stack) < $array[$i]){
            $k++;
            $result[end($stack)] = $k;
            array_pop($stack);

        }
        array_push($stack, $array[$i]);
    }
    return $result;
}
var_dump(zhan([3,2,4,0,6,7,8]));